package com.taptech.doufu.ui.activity.story

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.taptech.doufu.R
import com.taptech.doufu.app.WeMediaApplication
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.ui.activity.HomeSearchActivity
import com.taptech.doufu.ui.activity.MyWorksActivity
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity
import com.taptech.doufu.ui.adapter.CommonFragmentPagerAdapter
import com.taptech.doufu.ui.fragment.story.StoryMainFragment
import com.taptech.doufu.ui.view.TTHomeViewPager
import com.taptech.doufu.ui.view.WaitDialog
import com.taptech.doufu.ui.view.tablayout.TabLayout
import com.taptech.doufu.util.GsonUtil
import com.taptech.doufu.weex.http.TFHttpUtil
import kotlinx.android.synthetic.main.activity_story_main.*
import java.util.*

class StoryMainActivity:DiaobaoBaseActivity() {

    lateinit var waitDialog: WaitDialog
    lateinit var mViewPager: TTHomeViewPager
    lateinit var tlTitle:TabLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_story_main)
        waitDialog = WaitDialog(this,R.style.updateDialog)
        waitDialog.show()
        mViewPager = findViewById(R.id.mViewPager)
        tlTitle = findViewById(R.id.tlTitle)
        ivEdit.setOnClickListener {
            MyWorksActivity.startActivity(this,1)
        }
        ivSearch.setOnClickListener {
            val intent = Intent()
            intent.setClass(this,HomeSearchActivity::class.java)
            intent.putExtra(Constant.POSITION,1)
            startActivity(intent)
        }
        getData()
    }


    fun getData() {
        val url = "http://api.doufu.la/single/types"
        TFHttpUtil.requestGet(url,null){json, error ->
            try {
                val data = json.getJSONObject("data")
                val list = GsonUtil.jsonToList(data.getJSONArray("list").toJSONString(), Array<Item>::class.java)
                updateUi(list)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            waitDialog.dismiss()
        }
    }

    fun updateUi(items: MutableList<Item>) {
        val list = mutableListOf<Fragment>()
        mViewPager.offscreenPageLimit = 10
        var normalColor = resources.getColor(R.color.text_color_2)
        var selectColor = resources.getColor(R.color.text_color_57)
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            normalColor = resources.getColor(R.color.text_dark)
            selectColor = resources.getColor(R.color.theme_main_dark)
        }
        tlTitle.setTabTextColors(normalColor, selectColor)
        tlTitle.setSelectedTabIndicatorColor(selectColor)
        tlTitle.tabMode = TabLayout.MODE_SCROLLABLE
        if (items != null && items.size > 0) {
            val titles = arrayOfNulls<String>(items.size)
            for (i in items.indices) {
                val item = items[i] ?: continue
                titles[i] = item.title
                list.add(StoryMainFragment.newInstance(item.url))
            }
            val adapter = CommonFragmentPagerAdapter(supportFragmentManager, list, titles)
            mViewPager.adapter = adapter
            tlTitle.setupWithViewPager(mViewPager)
        }
    }

    data class Item(val title:String = "",val url:String = "")

    companion object{
        fun startActivity(context: Context) {
            val intent = Intent()
            intent.setClass(context,StoryMainActivity::class.java)
            context.startActivity(intent)
        }
    }
}